package com.wsy.controller;



import com.wsy.Pojo.User;
import com.wsy.service.UserService;
import com.wsy.utils.Result;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Controller
public class UserController {

    @Resource
    private UserService userService;

    @GetMapping("/index")
    public String index(Model model){
        return "UserList";
    }

//    @GetMapping("/getAll")
//    public void getAll(HttpServletResponse response) throws IOException {
//
//        List<User> list=userService.getAll();
//        PrintWriter out=response.getWriter();
//        Result result=Result.success(list);
//        response.setContentType("application/json;charset=UTF-8");
//        //利用Jackson组件把Java对象转化为json格式字符串
//        ObjectMapper objectMapper=new ObjectMapper();
//        objectMapper.registerModule(new JavaTimeModule());
//        String json=objectMapper.writeValueAsString(result);
//        out.print(json);
//        out.close();
//    }

    @GetMapping(value = "/getAll", produces = "application/json;charset=UTF-8")
    @ResponseBody//替代PrintWriter out=response.getWriter();
    public Result getAll(HttpServletResponse response) throws IOException {

        List<User> list=userService.getAll();
        Result result=Result.success(list);
//        //利用Jackson组件把Java对象转化为json格式字符串
//        ObjectMapper objectMapper=new ObjectMapper();
//        objectMapper.registerModule(new JavaTimeModule());
//        String json=objectMapper.writeValueAsString(result);
        //String json =JSON.toJSONString(result);

        return result;
    }



}
